<?php
/**
*
* ClassName: Apisearch_parser_model
*
* 解析 开放平台 的search结果数据
*
* @author hejigui <hejigui@soften.cn>
*
*/
class Apisearch_parser_model extends CI_Model{

	public function __construct() {
		parent::__construct();
        $this->load->helper('MY_common_helper');
        $this->load->helper('MY_http_helper');
	}

	/**
	* 解析新浪平台 search接口返回的数据
	* 返回我们需要的结构数组
	*
	* @access public
	* @param array $data 新浪微博 返回结构化数据 [Must]
	* @return array
	*/
	public function parser_search_sina_api($data) {

		if(!is_array($data) || empty($data)){
			log_message('error', "parser_search_sina_api param is error !! data:".var_export($data,true));
			return false;
		}

		$this->config->load('openplatforms');
		$feeduser_tmp = $this->config->item('feeduser');
		$apisearch_search = $feeduser_tmp['apisearch'];
		$source_myid = $apisearch_search['sina']['source_myid'];

		$tweet_key = count($data);

		$this->load->helper('common_helper');
		foreach ($data as $key => $value) {

			$loop = 1;
			for($i=1; $i<=$loop; $i++)
			{
				$final_data[$key]['_id'] = strval($value['id']);
				$final_data[$key]['text'] = $value['text'];
				$final_data[$key]['ctime'] = my_strtotime($value['created_at']);
				$final_data[$key]['source'] = $value['source'];
				$final_data[$key]['reposts_count'] = $value['reposts_count'];
				$final_data[$key]['comments_count'] = $value['comments_count'];
				$final_data[$key]['attitudes_count'] = $value['attitudes_count'];
				$final_data[$key]['url'] = "http://api.t.sina.com.cn/{$value['user']['idstr']}/statuses/{$value['idstr']}";
				$final_data[$key]['thumbnail_pic'] = $value['thumbnail_pic'];
				$final_data[$key]['bmiddle_pic'] = $value['bmiddle_pic'];
				$final_data[$key]['original_pic'] = $value['original_pic'];
				$final_data[$key]['addtime'] = time();
				$final_data[$key]['source_myid'] = $source_myid;
				$final_data[$key]['retweeted_id'] = '';
				$final_data[$key]['retweeted_uid'] = '';
				$final_data[$key]['retweeted_name'] = '';

				//用户信息
				$final_data[$key]['user']['id'] = strval($value['user']['id']);
				//$final_data[$key]['user']['screen_name'] = $value['user']['screen_name'];
				$final_data[$key]['user']['name'] = $value['user']['name'];
				$final_data[$key]['user']['description'] = $value['user']['description'];
				$final_data[$key]['user']['profile_url'] = "http://weibo.com/{$value['user']['profile_url']}";
				$final_data[$key]['user']['header_url'] = $value['user']['profile_image_url'];
				$final_data[$key]['user']['followers_count'] =str_replace('万','0000', $value['user']['followers_count']);
				$final_data[$key]['user']['friends_count'] = $value['user']['friends_count'];
				$final_data[$key]['user']['verified'] = $value['user']['verified']? 1:0;  //true or false

				// 看是不是转播
				if (isset($value['retweeted_status']))
				{
					$final_data[$key]['retweeted_id'] = $value['retweeted_status']['id'];
					$final_data[$key]['retweeted_uid'] = $value['retweeted_status']['user']['id'];
					$final_data[$key]['retweeted_name'] = $value['retweeted_status']['user']['name'];

					$key = $tweet_key;
					$tweet_key++;
					$value = $value['retweeted_status'];
					$loop = 2;
				}
			}
		}

		return $final_data;
	}

    /**
     * 解析搜狐平台 friends search接口返回的数据
     * 返回我们需要的结构数组
     * 注意此接口传入的数组元素为对象
     *
     * @access public
     * @param array $response_data 搜狐平台 返回结构化数据 [Must]
     * @return array
     */
    public function parser_search_sohu_api($source_myid, $response_data, $tweet = FALSE) {
        $result_array = array();
        $response_data = $response_data['statuses'];
        foreach ($response_data as $value) {
            $current_data = array();
            $current_data['_id'] = strval($value['id']);
            $current_data['text'] = $value['text'];
            $current_data['ctime'] = my_strtotime($value['created_at']);
            $current_data['source'] = $value['source'];
            $current_data['reposts_count'] = 0;
            $current_data['comments_count'] = 0;
            $current_data['attitudes_count'] = 0;
            $current_data['url'] = "http://t.sohu.com/m/".strval($value['id']);
            $current_data['thumbnail_pic'] = $value['small_pic'];
            $current_data['bmiddle_pic'] = $value['middle_pic'];
            $current_data['original_pic'] = $value['original_pic'];
            $current_data['addtime'] = time();
            $current_data['source_myid'] = $source_myid;
            if(array_key_exists('retweeted_status', $value) && count($value['retweeted_status']) > 0) {
                $current_data['retweeted_id'] = $value['retweeted_status']['id'];
                $current_data['retweeted_uid'] = $value['retweeted_status']['user']['id'];
                $current_data['retweeted_name'] = $value['retweeted_status']['user']['screen_name'];
            } else {
                $current_data['retweeted_id'] = '';
                $current_data['retweeted_uid'] = '';
                $current_data['retweeted_name'] = '';
            }

            //用户信息
            $current_data['user']['id'] = strval($value['user']['id']);
            $current_data['user']['name'] = $value['user']['screen_name'];
            $current_data['user']['description'] = $value['user']['description'];
            $current_data['user']['profile_url'] = "http://t.sohu.com/u/".strval($value['user']['id']);
            $current_data['user']['header_url'] = $value['user']['profile_image_url'];
            $current_data['user']['followers_count'] = $value['user']['followers_count'];
            $current_data['user']['friends_count'] = $value['user']['friends_count'];
            $current_data['user']['verified'] = $value['user']['verified'] ? 1:0;   //true or false

            if($tweet === TRUE) {
                return $current_data;
            }

//            $result_array[$current_data['_id']] = $current_data;
            $result_array[] = $current_data;

            if(array_key_exists('retweeted_status', $value) && count($value['retweeted_status']) > 0) {
                $tweet = $this->parser_search_sohu_api($source_myid, array('statuses' =>array($value['retweeted_status'])), TRUE);
//                $result_array[$tweet['_id']] = $tweet;
                $result_array[] = $tweet;
            }
        }

        return $result_array;
    }

	/**
	* 解析腾讯平台 search接口返回的数据
	* 返回我们需要的结构数组
	* 此处若接到的列表中有转发数据..会把转发的微博抽出来,并入列表
	*
	* @access public
	* @param array $data 腾讯微博给出的response [Must]
	* @return array
	*/
	public function parser_search_tencent_api($source_myid, $response_data) {
        $info_list = $response_data['data']['info'];
		$tweet_key = count($info_list);
		foreach ($info_list as $key => $value) {

			$loop = 1;
			for($i=1; $i<=$loop; $i++)
			{
				$final_data[$key]['_id'] = strval($value['id']);
				$final_data[$key]['text'] = $value['text'];
				$final_data[$key]['ctime'] = $value['timestamp'];
				$final_data[$key]['source'] = isset($value['fromurl'])? "<a href={$value['fromurl']} >{$value['from']}</a>": '';
				$final_data[$key]['reposts_count'] = $value['count'];
				$final_data[$key]['comments_count'] = $value['mcount'];
				$final_data[$key]['attitudes_count'] = 0;
				$final_data[$key]['url'] = 'http://t.qq.com/p/t/'.strval($value['id']);
				if($value['image']){
					$final_data[$key]['thumbnail_pic'] = $value['image'][0].'/160';
					$final_data[$key]['bmiddle_pic'] = $value['image'][0].'/460';
					$final_data[$key]['original_pic'] = $value['image'][0].'/2000';
				} else {
                    $final_data[$key]['thumbnail_pic'] = '';
                    $final_data[$key]['bmiddle_pic'] = '';
                    $final_data[$key]['original_pic'] = '';
                }

				$final_data[$key]['addtime'] = time();
				$final_data[$key]['source_myid'] = $source_myid;
				$final_data[$key]['retweeted_id'] = '';
				$final_data[$key]['retweeted_uid'] = '';
				$final_data[$key]['retweeted_name'] = '';


				//用户信息
				$final_data[$key]['user']['id'] = $value['name']; //$value['openid'];
				$final_data[$key]['user']['openid'] = $value['openid'];
				//$final_data[$key]['user']['screen_name'] = $value['nick'];
				$final_data[$key]['user']['name'] = $value['nick'];
				$final_data[$key]['user']['description'] = '';
				$final_data[$key]['user']['profile_url'] = "http://t.qq.com/{$value['name']}";
				$final_data[$key]['user']['header_url'] = empty($value['head']) ? '' : $value['head'].'/50';
				$final_data[$key]['user']['followers_count'] = '';
				$final_data[$key]['user']['friends_count'] = '';

				$final_data[$key]['user']['verified'] = $value['isvip']; //0-不是，1-是

				// 看是不是转播
				if (isset($value['source']) && !empty($value['source']))
				{
					$final_data[$key]['retweeted_id'] = $value['source']['id'];
					$final_data[$key]['retweeted_uid'] = $value['source']['name'];
					$final_data[$key]['retweeted_name'] = $value['source']['nick'];

					$key = $tweet_key;
					$tweet_key++;
					$value = $value['source'];
					$loop = 2;
				}
			}

		}

		return $final_data;
	}

	/**
	* 解析网易平台 search接口返回的数据
	* 返回我们需要的结构数组
	*
	* @access public
	* @param array $data 网易平台 返回结构化数据 [Must]
	* @return array
	*/
	public function parser_search_netease_api($data) {
		if(!is_array($data) || empty($data)){
			log_message('error', "parser_search_tencent_api param is error !! data:".var_export($data,true));
			return false;
		}

		$this->config->load('openplatforms');
		$feeduser_tmp = $this->config->item('feeduser');
		$apisearch_search = $feeduser_tmp['apisearch'];
		$source_myid = $apisearch_search['netease']['source_myid'];

		$tweet_key = count($data);

		$this->load->helper('common_helper');
		foreach ($data as $key => $value) {

			$final_data[$key]['_id'] = strval($value['id']);
			$final_data[$key]['text'] = $value['text'];
			$final_data[$key]['ctime'] = strtotime($value['created_at']);  //Tue Aug 17 12:46:16 +0800 2010
			$final_data[$key]['source'] = strip_tags($value['source']);
			$final_data[$key]['reposts_count'] = $value['retweet_count'];
			$final_data[$key]['comments_count'] = $value['comments_count'];
			$final_data[$key]['attitudes_count'] = '';
			$final_data[$key]['retweeted_id'] = $value['root_in_reply_to_status_id'];
			$final_data[$key]['url'] = 'http://t.163.com/pocophoto/status/'.strval($value['id']);;
			$final_data[$key]['thumbnail_pic'] = '';
			$final_data[$key]['bmiddle_pic'] = '';
			$final_data[$key]['original_pic'] = '';
			$final_data[$key]['addtime'] = time();
			$final_data[$key]['source_myid'] = $source_myid;
			$final_data[$key]['retweeted_id'] = '';
			$final_data[$key]['retweeted_uid'] = '';
			$final_data[$key]['retweeted_name'] = '';

			//用户信息
			$final_data[$key]['user']['id'] = $value['user']['id'];
			//$final_data[$key]['user']['screen_name'] = $value['retweet_user_screen_name'];
			$final_data[$key]['user']['name'] = $value['user']['name'];
			$final_data[$key]['user']['description'] = $value['user']['description'];
			$final_data[$key]['user']['profile_url'] = 'http://t.163.com/'.$value['user']['screen_name'];
			$final_data[$key]['user']['header_url'] = $value['user']['profile_image_url'];
			$final_data[$key]['user']['followers_count'] = $value['user']['followers_count'];
			$final_data[$key]['user']['friends_count'] =   $value['user']['friends_count'];
			$final_data[$key]['user']['verified'] = $value['user']['verified']? 1:0; //true or false

			// 看是不是转播
			if ($value['in_reply_to_status_id'])
			{
				$final_data[$key]['retweeted_id'] = strval($value['in_reply_to_status_id']);
				$final_data[$key]['retweeted_uid'] = $value['in_reply_to_user_id'];
				$final_data[$key]['retweeted_name'] = $value['in_reply_to_user_name'];

				$key = $tweet_key;
				$tweet_key++;

				$final_data[$key]['_id'] = strval($value['in_reply_to_status_id']);
				$final_data[$key]['text'] = $value['in_reply_to_status_text'];
				$final_data[$key]['ctime'] = $value['retweet_created_at']? my_strtotime($value['retweet_created_at']): my_strtotime($value['created_at']);  //Tue Aug 17 12:46:16 +0800 2010
				$final_data[$key]['source'] = strip_tags($value['source']);
				$final_data[$key]['reposts_count'] = $value['root_retweet_count'];
				$final_data[$key]['comments_count'] = $value['root_comments_count'];
				$final_data[$key]['attitudes_count'] = '';
				$final_data[$key]['url'] = 'http://t.163.com/pocophoto/status/'.$value['in_reply_to_status_id'];
				$final_data[$key]['thumbnail_pic'] = '';
				$final_data[$key]['bmiddle_pic'] = '';
				$final_data[$key]['original_pic'] = '';
				$final_data[$key]['addtime'] = time();
				$final_data[$key]['source_myid'] = $source_myid;

				//用户信息
				$final_data[$key]['user']['id'] = $value['in_reply_to_user_id'];
				$final_data[$key]['user']['name'] = $value['in_reply_to_user_name'];
				$final_data[$key]['user']['profile_url'] = 'http://t.163.com/'.$value['root_in_reply_to_screen_name'];

			}

		}

		return $final_data;
	}


	/**
	* 解析xx平台HTML搜索结果页面内容，提出标准微博内容数组
	*
	* @access public
	* @param array $platform_info xx微博搜索平台配置信息 [Must]
	* @param string $html xx微博搜索结果HTML代码 [Must]
	* @return   array 微博结果数组
	*/
	public function parser_xxx($platinfo, $html) {
		if(empty($platform_info) || !is_array($platform_info) || empty($html)){
			log_message('error', "parser_xxx param is error !! platform_info:{$platform_info} --- html_content:{$html}");
			return false;
		}

		//最张结构化数据结构...后边相关 其它字段自行调整
		$arrdata = array(
			'id' => '微博 id',
			'uid' => '发布作者ID',
			'text' => '微博内容',
			'ctime' => '微博内容发布时间',
			'addtime' => '抓取到时间',
		);

		return $arrdata;

	}
}

?>